package com.erp.admin.common.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.erp.base.base.BaseController;
import com.erp.base.base.Page;
import com.erp.base.common.entity.Dict;
import com.erp.base.common.service.DictService;
import com.erp.base.common.utils.R;

/**
 * 字典表
 * @author whw
 */

@Controller
@RequestMapping("/common/dict")
public class DictController extends BaseController {
	@Autowired
	private DictService dictService;

	@GetMapping()
	@RequiresPermissions("common:dict:dict")
	String dict() {
		return "common/dict/dict";
	}

	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("common:dict:dict")
	public Page<Dict> list(Dict dict,HttpServletRequest request,HttpServletResponse response) {
		Page<Dict>page=Page.bootstrapPage(request, response);
		page=dictService.findPage(page, dict);
		return page;
	}

	@GetMapping("/add")
	@RequiresPermissions("common:dict:add")
	String add() {
		return "common/dict/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("common:dict:edit")
	String edit(@PathVariable("id") Long id, Model model) {
		Dict dict = dictService.get(id);
		model.addAttribute("dict", dict);
		return "common/dict/edit";
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("common:dict:add")
	public R save(Dict dict,Model model) {
		if(!beanValidator(model, dict)){
			return R.error((String)model.asMap().get("message"));
		}
		try {
			dictService.save(dict);
			return R.ok();
		} catch (Exception e) {
			logger.error("字典保存失败", e);
			return R.error();
		}
	}

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("common:dict:edit")
	public R update(Dict dict,Model model) {
		if(!beanValidator(model, dict)){
			return R.error((String)model.asMap().get("message"));
		}
		try {
			dictService.save(dict);
			return R.ok();
		} catch (Exception e) {
			logger.error("字典保存失败", e);
			return R.error();
		}
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ResponseBody
	@RequiresPermissions("common:dict:remove")
	public R remove(Long id) {
		try {
			dictService.delete(id);
			return R.ok();
		} catch (Exception e) {
			logger.error("字典删除失败，id:"+id, e);
			return R.error();
		}
	}

	/**
	 * 删除
	 */
	@PostMapping("/batchRemove")
	@ResponseBody
	@RequiresPermissions("common:dict:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids) {
		try {
			dictService.deleteBatch(Arrays.asList(ids));
			return R.ok();
		} catch (Exception e) {
			logger.error("字典删除失败，ids:"+ids, e);
			return R.error();
		}
	}

	@GetMapping("/type")
	@ResponseBody
	public List<Dict> listType() {
		return dictService.listType();
	};

	// 类别已经指定增加
	@GetMapping("/add/{type}/{description}")
	@RequiresPermissions("common:dict:add")
	String addD(Model model, @PathVariable("type") String type, @PathVariable("description") String description) {
		model.addAttribute("type", type);
		model.addAttribute("description", description);
		return "common/dict/add";
	}

	@ResponseBody
	@GetMapping("/list/{type}")
	public List<Dict> listByType(@PathVariable("type") String type) {
		List<Dict> dictList = dictService.listByType(type);
		return dictList;
	}
	
	@ResponseBody
	@GetMapping("/list/types")
	public Map<String, List<Dict>> listByTypes(@RequestParam("types") String ...type) {
		return dictService.listByTypes(type);
	}
}
